/*********************************************************************
Programmer:     Danny Vargas
Date:           04/26/09
File:           Disk.h

Description:	A simple helper class for both the assembler and 
				VMachine.

Implementation: Only reads/writes one line at a time. The mundane
				tasks are covered by this class while the functions
				that call it must do the iterations for it to do 
				multiple lines.

Updated:		05/23/09
Changes:		New Class: File
				File class holds the information for a single file on 
				disk including filename, type, privilege, and simple
				postitional data. The class is used exclusively by Disk
				to open a file and perform operations much like PCB, OS,
				and VMachine.

Udpated:		06/10/09
Changes			Now has psuedo line position and row data. This is for 
				vector file inputs.

				Also generates an index of file locations for each line
				of object code in .o files. Very useful for treating 
				.o files as frames.
**********************************************************************/

#ifndef DISK_H
#define DISK_H

#include <fstream>
#include <vector>
#include <string>
#include <stdio.h>
using namespace std;

class File
{
	friend class Disk;
	private:
		string fileName;
		string fileType;
		string privilege;
		int linePos;
		int lineRow;
		vector<int> index;
		bool createIndex();

	public:
		File () { linePos = lineRow = 0; fileName = ""; privilege = ""; }
		File (const string, string);
		File (const File &);
		
		// File info
		string type () const;
		void changePriv (string);
		
		// Single Line Info
		int getLinePos () const { return linePos; }
		void incLinePos () { linePos++; }
		void resetLinePos () { linePos=0; }

		int getLineRow () const { return lineRow; }
		void incLineRow () { lineRow++; }

		int indexLine (const int) const;
		int indexLength () const { return index.size(); }

		void delFile();
};

class Disk
{
	private:
		fstream* stream;
		string fileType;
		bool writeP;
		bool readP;
		vector<string> line;

	public:
		Disk ();
		Disk (const File obj);
		Disk (string name, string privilege);
		~Disk(); 

		bool open (const File obj);
		bool open (string name, string privilege);
		bool open (string name, string privilege, string type);
		void close ();
		
		vector<string> getLine () const;

		string type () const;

		void setRead (int);
		void setWrite (int);
		int readLine ();

		template<typename T> bool writeLine(T line) 
		{
			if(!(*stream).is_open() || readP)
				return false;
			(*stream) << line << '\n';
			return true;
		}
};

#endif
